package com.blog.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.entity.SysRole;
import com.blog.entity.SysRoleMenu;
import com.blog.mapper.SysRoleMapper;
import com.blog.mapper.SysRoleMenuMapper;
import com.blog.service.SysRoleMenuService;
import com.blog.service.SysRoleService;
import com.blog.util.dto.AjaxResult;
import com.blog.util.tree.BaseLayTree;
import com.blog.util.tree.LayTreeVo;
import com.blog.util.tree.TreeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cgx
 * @Date 2022/9/1 21:53
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {


    @Resource
    SysRoleMapper roleMapper;

    @Resource
    SysRoleMenuService roleMenuService;


    @Override
    public AjaxResult getMenuByRole() {
        List<LayTreeVo> list = roleMapper.getMenuInfo();
        return AjaxResult.success(TreeUtil.getLayTree(list).get(0).getChildren()) ;
    }

    @Override
    public void saveRoleMenu(Long roleId, Long[] menuIds) {
        List<SysRoleMenu> list = new ArrayList<>();
        for (Long menuId : menuIds) {
            SysRoleMenu roleMenu = new SysRoleMenu(roleId,menuId);
            list.add(roleMenu);
        }
        roleMenuService.saveBatch(list);
    }

    @Override
    public SysRole getRoleInfo(Long roleId) {
        SysRole byId = this.getById(roleId);
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",byId.getRoleId());
        //角色拥有的权限
        List<SysRoleMenu> roleMenuList = roleMenuService.list(queryWrapper);
        //获取所有的菜单
        List<LayTreeVo> list = roleMapper.getMenuInfo();
        //先对菜单进行选中操作
        for (LayTreeVo layTree : list) {
            for (SysRoleMenu roleMenu : roleMenuList) {
                if (layTree.getId().equals(roleMenu.getMenuId())){
                    layTree.setChecked(true);
                }
            }
        }

        List<LayTreeVo> children = TreeUtil.getLayTree(list).get(0).getChildren();
        //遍历所有菜单，设置选中状态
        byId.setMenuTree(children);
        return byId;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRole(List<Long> roleIds) {
        String roleIdStr = StringUtils.join(roleIds, ",");
        //删除角色信息
        roleMapper.deleteRole(roleIdStr);
        //删除角色菜单权限表
        roleMenuService.deleteMenuRole(roleIdStr);
    }

    @Override
    public void updateRole(SysRole role) {
        //更新基本的角色信息
        this.updateById(role);
        //先删除掉角色原先拥有的权限
        roleMenuService.removeById(role.getRoleId());
        //重新插入角色权限
        List<SysRoleMenu> list = new ArrayList<>();
        for (Long menuId : role.getMenuIds()) {
            SysRoleMenu roleMenu = new SysRoleMenu(role.getRoleId(),menuId);
            list.add(roleMenu);
        }
        roleMenuService.saveBatch(list);
    }
}
